package com.LiL.Wayne.Activity.activity;


import java.io.FileInputStream;


import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.LiL.Wayne.Activity.R;
import com.LiL.Wayne.Activity.R.anim;
import com.LiL.Wayne.Activity.R.id;
import com.LiL.Wayne.Activity.R.layout;
import com.LiL.Wayne.Activity.activity.PlayerVideoLyrics.ImageAdapter;
import com.LiL.Wayne.Activity.activity.PlayerVideoLyrics.SharePm;
import com.LiL.Wayne.Activity.activity.PlayerVideoLyrics.ViewHolder;
import com.audamob.audasingers.model.Music;
import com.audamob.audasingers.model.PlayList;
import com.audamob.audasingers.model.Song;


import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.util.TypedValue;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.webkit.WebSettings;
import android.webkit.WebSettings.PluginState;
import android.webkit.WebView;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class PlayList_Interface extends Activity {

	private SimpleDateFormat FormatterDuration = new SimpleDateFormat("mm:ss");
	 ArrayList<Music> MusicList;
	
	
        ListView ListVideo;
        
        
        public ArrayList<PlayList> restorePlayListList() throws IOException, ClassNotFoundException {
			FileInputStream fin = new FileInputStream(getCacheDir().getAbsolutePath()+"/playlistlist");
			ObjectInputStream in = new ObjectInputStream(fin);
			ArrayList<PlayList> object = (ArrayList<PlayList>)in.readObject();
			in.close();
			return object;
		}
	 public  void sauvegardPlayListList(ArrayList<PlayList> s) throws IOException {
			FileOutputStream fout = new FileOutputStream(getCacheDir().getAbsolutePath()+"/playlistlist");
			ObjectOutputStream out = new ObjectOutputStream(fout);
			
			out.writeObject(s);
			out.close();
		}
	 
	 int position;
	 String NamePlaylist;
	 ArrayList<PlayList>plll;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
        	// TODO Auto-generated method stub
        	super.onCreate(savedInstanceState);
        	
        	setContentView(R.layout.playlist_music);
        	
    		Bundle b=getIntent().getExtras();
    		position=b.getInt("position");
    		
    		
    		plll=new ArrayList<PlayList>();
    		try {
				plll=restorePlayListList();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
    		MusicList=plll.get(position).getListMusic();   
    		NamePlaylist=plll.get(position).getName();
    	
                
    		ListVideo=(ListView)findViewById(R.id.ListAppM);
    		ListVideo.setAdapter(new AppAdapter());
            	
    		   edit_List = (RelativeLayout)findViewById(R.id.layout_edit_edit);
               edit_List.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						Edit_List();
					}

					
				});
            	
               edit_done = (RelativeLayout)findViewById(R.id.layout_edit);
               edit_done.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						Done_edit_case();
					}

					
				});
               
               RelativeLayout play_all=(RelativeLayout)findViewById(R.id.Layout_Add_video);
               play_all.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					Intent i=new Intent(getBaseContext(),MusicPlayer.class);
					i.putExtra("name","");
					i.putExtra("album","");
					i.putExtra("mode", 2);
					i.putExtra("ID", position);
					startActivity(i);
					overridePendingTransition(R.anim.push_down_in,R.anim.push_down_out);
				}
			});
            
              TextView tx=(TextView)findViewById(R.id.Title);
              tx.setText("Playlist : "+NamePlaylist);
         
        }
        public float Dip_To_Px(int val){

			Resources r = getResources();
			float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, val, r.getDisplayMetrics());
			return px;
    	  }
        
        RelativeLayout edit_done, edit_List;
       
        boolean ModePlaylist=false;
        PlayList pl=null;
        int plID=0;
        ArrayList<PlayList> ArrayPlaylist;
      
        @Override
   	 public void onResume(){
   	    super.onResume();
        	// TODO Auto-generated method stub
        
        }
     public class AppAdapter extends BaseAdapter {
      		private LayoutInflater mInflater;

      		public AppAdapter() {mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      		}

      		public int getCount() {
      			return MusicList.size();
      		}

      		public Object getItem(int position) {
      			return position;
      		}

      		public long getItemId(int position) {
      			return position;
      		}

      		public View getView(final int position, View convertView, ViewGroup parent) {
      			final ViewHolderImage holder;
      			if (convertView == null) {
      				holder = new ViewHolderImage();
      				
      				convertView = mInflater.inflate(R.layout.auda_item_music, null);
      				
      				holder.Logo = (ImageView) convertView.findViewById(R.id.Icon);
      				holder.Name=(TextView)convertView.findViewById(R.id.nam);
      			
      				
      				convertView.setTag(holder);
      			}
      			else {
      				holder = (ViewHolderImage) convertView.getTag();
      			}
      			
      			holder.Logo.setId(position);
      			holder.Name.setId(position);
      			
      			convertView.findViewById(R.id.ItemSong).setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						// TODO Auto-generated method stub
						Intent i=new Intent(getBaseContext(),MusicPlayer.class);
						i.putExtra("name",MusicList.get(position).getName());
						i.putExtra("album", MusicList.get(position).getUrl());
						i.putExtra("feat",MusicList.get(position).getFeatring());
						i.putExtra("mode", 1);
						startActivity(i);
						overridePendingTransition(R.anim.push_down_in,R.anim.push_down_out);
						
			
					}
				});
      			
      		
      			holder.Name.setText(MusicList.get(position).getName());
      			holder.id = position;
      		
      			return convertView;
      		}
      	}
     class ViewHolderImage {
	  		ImageView Logo;
	  		TextView Name;
	  		
	  		int id;
  	 }

   
	 
     int EditSongNameTextlength=0;
	 int EditAlbumNameTextlength=0;
	 private static boolean[] HideImageImageselection;
	
	 
	 
	 public static Boolean State_Edit=false;
    	 
	 public  void Mode_Edit_On(){
         	HideImageImageselection=new boolean[MusicList.size()];
         	
         	AppAdapterEdit ad=new AppAdapterEdit();
         	
         	ListVideo.setAdapter(ad);
         	State_Edit=true;
	 }
	 public   void Edit_List() {
			// TODO Auto-generated method stub
			
				Mode_Edit_On();
				 edit_done.setVisibility(View.VISIBLE);
				 edit_List.setVisibility(View.INVISIBLE);
				
			
		}
     public   void Done_edit_case() {
			// TODO Auto-generated method stub
			
				Mode_Edit_of();
				 edit_done.setVisibility(View.INVISIBLE);
				 edit_List.setVisibility(View.VISIBLE);
				
			
		}
     public void Mode_Edit_of(){
		  ArrayList<Music> list=new ArrayList<Music>();
        
         
         for(int j=0;j<MusicList.size();j++){
      	   
       	  if(HideImageImageselection[j]){
      		   
      		         		  
       	  }else {
				list.add(MusicList.get(j));
       	  }
       	  
         }
         
         PlayList pl=new PlayList(NamePlaylist, list);
       	plll.remove(position);
       	plll.add(position, pl);
       	try {
				sauvegardPlayListList(plll);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
      
    	
       	update_your_self();
    	
		  }
     public void update_your_self(){
     	plll.clear();
     	plll=new ArrayList<PlayList>();
 		try {
				plll=restorePlayListList();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
 		MusicList=plll.get(position).getListMusic();   
 		NamePlaylist=plll.get(position).getName();
 	     
 		ListVideo=(ListView)findViewById(R.id.ListAppM);
 		ListVideo.setAdapter(new AppAdapter());
     	
     }
     
	 public class AppAdapterEdit extends BaseAdapter {
          		private LayoutInflater mInflater;

          		public AppAdapterEdit() {mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
          		}

          		public int getCount() {
          			return MusicList.size();
          		}

          		public Object getItem(int position) {
          			return position;
          		}

          		public long getItemId(int position) {
          			return position;
          		}

          		public View getView(final int position, View convertView, ViewGroup parent) {
          			ViewHolderImage_edit holder;
          			if (convertView == null) {
          				holder = new ViewHolderImage_edit();
          				
          				convertView = mInflater.inflate(R.layout.auda_item_music_edit, null);
          				holder.checkbox = (CheckBox) convertView.findViewById(R.id.checkbox);
          			
          				holder.Name=(TextView)convertView.findViewById(R.id.nam);
          			
          				
          				convertView.setTag(holder);
          			}
          			else {
          				holder = (ViewHolderImage_edit) convertView.getTag();
          			}
          			holder.checkbox.setId(position);
          			
          			holder.Name.setId(position);
          		
          			holder.checkbox.setOnClickListener(new OnClickListener() {
          				
          				public void onClick(View v) {
          					// TODO Auto-generated method stub
          					CheckBox cb = (CheckBox) v;
          					int id = cb.getId();
          					if (HideImageImageselection[id]){
          						cb.setChecked(false);
          						HideImageImageselection[id] = false;
          					} else {
          						cb.setChecked(true);
          						HideImageImageselection[id] = true;
          					}
          				}
          			});
          			
          			
          			holder.checkbox.setChecked(HideImageImageselection[position]);
          			holder.Name.setText(MusicList.get(position).getName());
          		
          			holder.id = position;
          		
          		
          			return convertView;
          		}
          	}
	 class ViewHolderImage_edit {
    	  		CheckBox checkbox;
    	  		
    	  		TextView Name;
    	  	
    	  		int id;
      	 }
	 @Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		  finish();
		  overridePendingTransition(R.anim.push_down_out_back,R.anim.push_down_in_back);
	}
	
}
